package com.hongxun.budget.ui.activity.ys;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.widget.DividerItemDecoration;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.greendao.DaoSession;
import com.hongxun.budget.greendao.EDeviceBuyEntityDao;
import com.hongxun.budget.greendao.EMaterialBuyEntityDao;
import com.hongxun.budget.greendao.EProblemexistEntityDao;
import com.hongxun.budget.ui.activity.BaseListActivity;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.XUtils;
import com.hongxun.entity.StatisticsItem;
import com.hongxun.greenentity.ys.EDeviceBuyEntity;
import com.hongxun.greenentity.ys.EMaterialBuyEntity;
import com.hongxun.greenentity.ys.EProblemexistEntity;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class YsStatisticsActivity extends BaseListActivity implements RadioGroup.OnCheckedChangeListener {

    DaoSession session;
    String uuid;
    List<StatisticsItem> datas = new ArrayList<>();
    SuperAdapter<StatisticsItem> adapter;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    int checkId = R.id.radioButton1;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.name)
    TextView name;

    @Override
    public int getMyListLayoutId() {
        return R.layout.activity_check_statistics;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        list.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        uuid = SPUtils.get(context, "project_ys_guid", "").toString();
        session = GreenDaoManager.getInstance().getmDaoSession();
        adapter.setData(datas);
        adapter.setEmptyDataView(R.layout.view_empty);
        radioGroup.setOnCheckedChangeListener(this);
        radioGroup.check(R.id.radioButton1);
        setDevice();
    }

    @Override
    public SuperAdapter getAdapter() {
        return adapter = new SuperAdapter<StatisticsItem>(R.layout.content_statistics_item) {
            @Override
            public void bindView(ViewHolder viewHolder, StatisticsItem statisticsItem, int i) {
                ((TextView) viewHolder.getView(R.id.name)).setText(statisticsItem.getName());
                ((TextView) viewHolder.getView(R.id.count)).setText(statisticsItem.getCount() + "");
                TextView textView = viewHolder.getView(R.id.price);
                if (checkId == R.id.radioButton3) {
                    textView.setVisibility(View.GONE);
                } else {
                    textView.setVisibility(View.VISIBLE);
                    textView.setText(statisticsItem.getPrice() + "");
                }
            }
        };
    }

    void setDevice() {
        datas.clear();
        List<String> strings = XUtils.distinctQuery(EDeviceBuyEntityDao.Properties.DBMidtype.columnName, EDeviceBuyEntityDao.TABLENAME
                , EDeviceBuyEntityDao.Properties.PGuid.columnName + " = '" + uuid + "' and " + EDeviceBuyEntityDao.Properties.DBDatastate.columnName
                        + " != '删除'");
        for (String mid : strings) {
            List<EDeviceBuyEntity> list = session.getEDeviceBuyEntityDao().queryBuilder().where(EDeviceBuyEntityDao.Properties.DBMidtype.eq(mid),
                    EDeviceBuyEntityDao.Properties.PGuid.eq(uuid), EDeviceBuyEntityDao.Properties.DBDatastate.notEq("删除")).list();
            StatisticsItem s = new StatisticsItem();
            s.setName(mid);
            for (EDeviceBuyEntity d : list) {
                s.setCount(s.getCount() + d.getDBCount());
                s.setPrice(s.getPrice() + d.getDBPriceall());
            }
            datas.add(s);
        }
        adapter.notifyDataSetChanged();
    }

    void setMaterial() {
        datas.clear();
        List<String> strings = XUtils.distinctQuery(EMaterialBuyEntityDao.Properties.MBMidtype.columnName, EMaterialBuyEntityDao.TABLENAME
                , EMaterialBuyEntityDao.Properties.PGuid.columnName + " = '" + uuid + "' and " + EMaterialBuyEntityDao.Properties.MBDatastate.columnName
                        + " != '删除'");
        for (String mid : strings) {
            List<EMaterialBuyEntity> list = session.getEMaterialBuyEntityDao().queryBuilder().where(EMaterialBuyEntityDao.Properties.MBMidtype.eq(mid),
                    EMaterialBuyEntityDao.Properties.PGuid.eq(uuid), EMaterialBuyEntityDao.Properties.MBDatastate.notEq("删除")).list();
            StatisticsItem s = new StatisticsItem();
            s.setName(mid);
            for (EMaterialBuyEntity d : list) {
                s.setCount(s.getCount() + d.getMBCount());
                s.setPrice(s.getPrice() + d.getMBPriceall());
            }
            datas.add(s);
        }
        adapter.notifyDataSetChanged();
    }

    void setQuantities() {
        datas.clear();
        //材料和设备的工程量
        List<String> strings = XUtils.distinctQuery(EProblemexistEntityDao.Properties.Smalltype.columnName, EProblemexistEntityDao.TABLENAME
                , EProblemexistEntityDao.Properties.Projectguid.columnName + " = '" + uuid + "' and " + EProblemexistEntityDao.Properties.Datastate.columnName
                        + " != '删除'");
        for (String mid : strings) {
            List<EProblemexistEntity> list = session.getEProblemexistEntityDao().queryBuilder().where(EProblemexistEntityDao.Properties.Smalltype.eq(mid),
                    EProblemexistEntityDao.Properties.Projectguid.eq(uuid), EProblemexistEntityDao.Properties.Datastate.notEq("删除")).list();
            StatisticsItem s = new StatisticsItem();
            s.setName(mid);
            for (EProblemexistEntity d : list) {
                s.setCount(s.getCount() + 1);
                s.setContent(d.getAccepter());
            }
            datas.add(s);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
        text.setVisibility(View.VISIBLE);
        checkId = i;
        switch (i) {
            case R.id.radioButton1:
                name.setText("设备名称");
                setDevice();
                break;
            case R.id.radioButton2:
                name.setText("材料名称");
                setMaterial();
                break;
            case R.id.radioButton3:
                text.setVisibility(View.GONE);
                name.setText("问题类别");
                setQuantities();
                break;
        }
    }

}
